#include "qstylesheetlist.h"

#include <QHeaderView>
#include <QPainter>

using namespace CORELIB;

QSize QStylesheetDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QSize size=QItemDelegate::sizeHint(option,index);
    size.setHeight(21);
    return size;
}

void QStylesheetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItemV3 opt=option;
    QColor c;

    c=QColor(255,230,191);
    if(opt.features & QStyleOptionViewItemV2::Alternate)
    {
        c=c.lighter(112);
    }


    painter->fillRect(option.rect,c);
    opt.state &=~ QStyle::State_HasFocus;
    QItemDelegate::paint(painter,opt,index);
}


QStylesheetList::QStylesheetList(QWidget *parent) :
    QTreeWidget(parent)
{
    this->setFrameStyle(QFrame::NoFrame);
    this->header()->setVisible(false);
    this->setMinimumSize(100,100);
    this->setItemDelegate(new QStylesheetDelegate);
    this->setAlternatingRowColors(true);
    this->setRootIsDecorated(false);

    QPalette p=this->palette();
    p.setColor(QPalette::Inactive,QPalette::Highlight,p.color(QPalette::Active,QPalette::Highlight));
    p.setColor(QPalette::Inactive,QPalette::HighlightedText,p.color(QPalette::Active,QPalette::HighlightedText));
    this->setPalette(p);

    connect(this,SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(clickedSlot(QTreeWidgetItem*)));

}

void QStylesheetList::addItem(const QString &text)
{
    QTreeWidgetItem *item=new QTreeWidgetItem(this);
    item->setText(0,text);
    item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
    m_items.append(item);
    if(this->currentItem()==NULL)
    {
        this->setCurrentItem(item);
        m_current=0;
    }
}

void QStylesheetList::clickedSlot(QTreeWidgetItem *item)
{
    if(m_items.indexOf(item)!=m_current)
    {
        m_current=m_items.indexOf(item);
        emit selectChanged(m_current);
    }
}
